import {get, put, post } from '@/utils/request';
export default {
    namespaced: true,
    state: {
        userAddress: [],
        defaultAddress: {}
    },
    mutations: {
        addUserAddress(state, payload) {
            state.userAddress = payload;
        }
    },
    actions: {
        // 获取地址列表
        getAddress({ commit }) {
            return new Promise((resolve, reject) => {
                get('/address')
                    .then(res => {
                        console.log(res.data.result);
                        commit('addUserAddress', res.data.result);
                        resolve(res.data);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        // 更新地址
        updateAddress({ state }, { requestMsg, id }) {
            return new Promise((resolve, reject) => {
                put(`/address/${id}`, requestMsg)
                    .then(res => {
                        console.log(res);
                        resolve(res);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        // 添加地址
        addAddress(state, payload) {
            return new Promise((resolve, reject) => {
                post('/address', payload)
                    .then(res => {
                        console.log(res);
                        resolve(res.result);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        }
    }
};